<template>
	<xn-form-container
		:title="formData.id ? '编辑事项' : '增加事项'"
		:width="500"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="事项类型：" name="type">
				<a-select v-model:value="formData.type" placeholder="请选择事项类型" :options="mattersTypeOptions" allowClear />
			</a-form-item>
			<a-form-item label="事项名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入事项名称" allow-clear />
			</a-form-item>
			<a-form-item label="事项详情：" name="detail">
				<a-textarea
					v-model:value="formData.detail"
					placeholder="请输入事项详情"
					allow-clear
					:auto-size="{ minRows: 4, maxRows: 8 }"
				/>
			</a-form-item>
			<a-form-item label="续费价格：" name="renewalFee">
				<a-input v-model:value="formData.renewalFee" placeholder="请输入续费价格" allow-clear />
			</a-form-item>
			<a-form-item label="续费类型：" name="renewalCycle">
				<a-select
					v-model:value="formData.renewalCycle"
					placeholder="请选择续费类型"
					:options="renewalCycleOptions"
					allowClear
				/>
			</a-form-item>
			<a-form-item label="起始日期：" name="startDate">
				<a-range-picker v-model:value="scopeDateArr" @change="dateChange" />
			</a-form-item>
			<a-form-item label="供应商：" name="provider">
				<a-input v-model:value="formData.provider" placeholder="请输入供应商" allow-clear />
			</a-form-item>
			<a-form-item label="通知天数：" name="noticeDate">
				<a-input v-model:value="formData.noticeDate" placeholder="请选择提前通知天数" allow-clear />
			</a-form-item>
			<a-form-item label="接收人：" name="userIdList">
				<xn-user-selector
					:org-tree-api="selectorApiFunction.orgTreeApi"
					:user-page-api="selectorApiFunction.userPageApi"
					data-type="object"
					v-model:value="formData.userIdList"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import renewalApi from '@/api/ams/renewal/renewalApi'
	import tool from '@/utils/tool'
	import userApi from '@/api/sys/userApi'
	import Promise from 'lodash-es/_Promise'
	import dayjs from 'dayjs'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const scopeDateArr = ref([dayjs().startOf('day'), dayjs().endOf('day')])
	const mattersTypeOptions = tool.dictList('MATTERS_TYPE')
	const renewalCycleOptions = tool.dictList('RENEWAL_CYCLE')
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			console.log(record)
			formData.value = Object.assign({}, record)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		type: [required('请选择事项类型')],
		name: [required('请输入事项名称')],
		renewalFee: [required('请输入续费价格')],
		renewalCycle: [required('请选择续费类型')],
		startDate: [required('请输入开始日期')],
		endDate: [required('请输入结束日期')],
		provider: [required('请输入供应商')],
		noticeDate: [required('请选择通知天数')]
	}
	const dateChange = (value) => {
		formData.value.startDate = value[0].format('YYYY-MM-DD')
		formData.value.endDate = value[1].format('YYYY-MM-DD')
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				if (formData.value.id) {
					renewalApi
						.edit(formData.value)
						.then((res) => {
							console.log(res)
							onClose()
							emit('successful')
						})
						.finally(() => {
							submitLoading.value = false
						})
				} else {
					renewalApi
						.add(formData.value)
						.then((res) => {
							console.log(res)
							onClose()
							emit('successful')
						})
						.finally(() => {
							submitLoading.value = false
						})
				}
			})
			.catch(() => {})
	}
	// 传递设计器需要的API
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return userApi.userOrgTreeSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userPageApi: (param) => {
			return userApi.userSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}
	defineExpose({
		onOpen
	})
</script>
